import time
# ----------------------------------------
try:
    from .cap import Cap
except:
    from cap import Cap
# --------------------------------------------------


class CapGroup:
    """Group of caps for a sets of specific task

    # Features: --------------------------------------------------
        1. Maintain multiple cap-objects in one group
        2. Schedule multiple caps do same task
    """

    def __init__(self, caps_info):
        self.caps = {
            cap_id: Cap(cap_id, uri)
            for cap_id, uri in caps_info.items()
        }

    def start(self, cb_func, args=None, kwargs=None, timeout=0):
        for cap in self.caps.values():
            cap.start(cb_func, args=(), kwargs=None, timeout=timeout)

    def stop(self):
        for cap in self.caps.values():
            cap.stop()

    def __del__(self):
        for cap in self.caps.values():
            cap._status = False
        del self.caps


if __name__ == '__main__':
    sideface_group = CapGroup({
        1: 0,
        2: 0,
        3: 0,
        4: 0
    })

    def print_frame_info(frame_info, *args, **kwargs):
        cap, offset, frame = frame_info
        # --------------------------------------------------
        if offset == 0:  # ----------------------------------------
            cap.local.frames = []
        elif offset == -1:  # ----------------------------------------
            print('%s 共 %s 帧' % (cap.id, len(cap.local.frames)))
        else:  # ----------------------------------------
            print(cap.id, offset)
            cap.local.frames.append(frame)

    sideface_group.start(print_frame_info, timeout=5)
    time.sleep(6)

